package com.jonluo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.jonluo.mapper.SysUserMapper;
import com.jonluo.model.Example;
import com.jonluo.model.SysUser;
import com.jonluo.service.SysUserService;
import com.jonluo.utils.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @author jonluo
 * Date 2017/9/15
 */
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = {"userList", "userCount"}, allEntries = true)})
    public int save(SysUser sysUser) {
        return sysUserMapper.insertSelective(sysUser);
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = {"userList", "userCount"}, allEntries = true)})
    public int saveBatch(List<SysUser> list) {
        int i = 0;
        for (SysUser sysUser : list) {
            i += save(sysUser);
        }
        return i;
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = {"userList"}, allEntries = true),
            @CacheEvict(value = "user", key = "'userId'+#p0.userId"),
            @CacheEvict(value = "user", key = "'userName'+#p0.username")})
    public int update(SysUser sysUser) {
        return sysUserMapper.updateByPrimaryKeySelective(sysUser);
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = {"userList", "userCount","user"}, allEntries = true)})
    public int delete(String id) {
        return sysUserMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = {"userList", "userCount","user"}, allEntries = true)})
    public int deleteBatch(String[] ids) {
        Example example = new Example();
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("userId", Arrays.asList(ids));
        return sysUserMapper.deleteByExample(example);
    }

    @Override
    @Cacheable(value = "user", key = "'userId'+#p0")
    public SysUser query(String id) {
        return sysUserMapper.selectByPrimaryKey(id);
    }

    @Override
    @Cacheable(value = "user", key = "'userName'+#p0")
    public SysUser queryByUserName(String userName) {
        return sysUserMapper.selectByUserName(userName);
    }


    @Override
    @Cacheable(value = "userList", key = "'userList'+#p0.toString()")
    public List<SysUser> queryList(PageInfo pageInfo) {
        return sysUserMapper.selectByExample(pageInfo2Example(pageInfo));
    }

    @Override
    @Cacheable(value = "userCount", key = "'userCount'+#p0.toString()")
    public int queryTotal(PageInfo pageInfo) {
        return sysUserMapper.countByExample(pageInfo2Example(pageInfo));
    }

    @Override
    @Cacheable(value = "userCount", key = "'userCount'")
    public int queryTotal() {
        return sysUserMapper.countByExample(null);
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = {"userList","user"}, allEntries = true)})
    public int updatePassword(String userId, String password, String newPassword) {
        Example example = new Example();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", userId);
        criteria.andEqualTo("password", password);
        SysUser user = new SysUser();
        user.setPassword(newPassword);
        return sysUserMapper.updateByExampleSelective(user, example);
    }

    @Override
    public List<String> getAllPerms(String userId) {
        return sysUserMapper.queryAllPerms(userId);
    }

    @Override
    public List<String> getAllDataPerms(String userId) {
        return sysUserMapper.queryAllDataPerms(userId);
    }


    @Override
    public List<String> getAllMenuId(String userId) {
        return sysUserMapper.queryAllMenuId(userId);
    }

    /**
     * @param pageInfo 分页参数转为条件封装example
     * @return example
     */
    private Example pageInfo2Example(PageInfo pageInfo) {
        Example example = new Example();
        Example.Criteria criteria = example.createCriteria();
        //分页
        if (pageInfo.getOffset() != 0 && pageInfo.getOffset() != 0) {
            example.setOffset(pageInfo.getOffset());
            example.setLimit(pageInfo.getLimit());
        }
        //查询条件
        JSONObject json = pageInfo.getJsonParams();
        /*if (StringUtils.isNotBlank(json.getString("mobile"))){
            criteria.andEqualTo("mobile",json.getString("mobile"));
        }*/
        if (StringUtils.isNotBlank(json.getString("username"))) {
            criteria.andLike("username", "%" + json.getString("username") + "%");
        }

        return example;
    }
}
